package com.cskaoyan.shopping.api;

import com.cskaoyan.mall.commons.result.ServiceCallResponse;
import com.cskaoyan.mall.commons.result.ServiceCallResponseUtil;
import com.cskaoyan.mall.dto.ClearCartItemRequest;
import com.cskaoyan.mall.dto.ClearCartItemServiceResponse;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailServiceResponse;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @auther cskaoyan
 */
@RestController
public class OrderApiImpl {

    @Autowired
    IProductService productService;

    @Autowired
    ICartService iCartService;


    @GetMapping(value = "/rpc/product/detail")
    ServiceCallResponse<ProductDetailDto> getProductDetail(@RequestParam("id") Long id) {
        ProductDetailDto productDetail = productService.getProductDetail(id);
        // 服务调用的返回值
        ServiceCallResponseUtil<ProductDetailDto> responseUtil = new ServiceCallResponseUtil<>();
        return responseUtil.ok(productDetail);
    }

    @DeleteMapping(value = "/rpc/cart/items")
    ClearCartItemServiceResponse clearCartItemByUserID(@RequestBody ClearCartItemRequest request) {
        ClearCartItemServiceResponse clearCartItemServiceResponse = new ClearCartItemServiceResponse();

        @NotEmpty List<Long> productIds = request.getProductIds();
        try {
            for (Long productId : productIds) {
                iCartService.deleteCartItem(request.getUserId(), productId);
            }
            clearCartItemServiceResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            clearCartItemServiceResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            return clearCartItemServiceResponse;
        } catch (Exception e) {
            e.printStackTrace();

            clearCartItemServiceResponse.setCode("404");
            clearCartItemServiceResponse.setMsg("下单后删除购物车商品失败");
            return clearCartItemServiceResponse;
        }

    }


}
